package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_AttackPacketPc;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.CheckUtil;
import com.lineage.server.world.World;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Attack extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Attack.class);
  
  public C_Attack() {}
  
  public C_Attack(byte[] abyte0, ClientExecutor client) {
    try {
      start(abyte0, client);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      pc.setFoeSlayer(false);
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport() && 
        !pc.isPrivateShop() && 
        !pc.isFreezeAtion())
        if (pc.getInventory().getWeight240() >= 197) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(110));
        } else {
          int result = pc.speed_Attack().checkInterval(
              AcceleratorChecker.ACT_TYPE.ATTACK);
          if (result == 2)
            _log.error("要求角色攻击:速度异常(" + pc.getName() + ")"); 
          if (pc.isInvisble())
            return; 
          if (pc.isInvisDelay())
            return; 
          if (pc.isParalyzedX())
            return; 
          if (pc.hasSkillEffect(8007))
            return; 
          if (pc.get_weaknss() != 0) {
            long h_time = Calendar.getInstance().getTimeInMillis() / 1000L;
            if (h_time - pc.get_weaknss_t() > 16L)
              pc.set_weaknss(0, 0L); 
          } 
          int targetId = 0;
          int locx = 0;
          int locy = 0;
          try {
            targetId = readD();
            locx = readH();
            locy = readH();
          } catch (Exception e) {
            over();
            return;
          } 
          if (locx == 0)
            return; 
          if (locy == 0)
            return; 
          L1Object target = World.get().findObject(targetId);
          if (target instanceof L1Character && target.getMapId() != pc.getMapId())
            return; 
          CheckUtil.isUserMap(pc);
          if (target instanceof L1NpcInstance) {
            int hiddenStatus = ((L1NpcInstance)target).getHiddenStatus();
            if (hiddenStatus == 1)
              return; 
            if (hiddenStatus == 2)
              return; 
          } 
          if (pc.hasSkillEffect(78)) {
            pc.killSkillEffectTimer(78);
            pc.startHpRegeneration();
            pc.startMpRegeneration();
          } 
          pc.killSkillEffectTimer(32);
          pc.delInvis();
          pc.setRegenState(1);
          if (target != null && !((L1Character)target).isDead()) {
            if (target instanceof L1PcInstance) {
              L1PcInstance tg = (L1PcInstance)target;
              pc.setNowTarget((L1Character)tg);
            } 
            target.onAction(pc);
            return;
          } 
          pc.setHeading(pc.targetDirection(locx, locy));
          pc.sendPacketsAll((ServerBasePacket)new S_AttackPacketPc(pc));
          return;
        }  
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
